package com.hc.acoustics.ui.activity

import android.content.Intent
import android.os.Bundle
import com.hc.acoustics.R
import com.hc.acoustics.base.BaseActivity
import com.hc.acoustics.util.FormulaUtil
import com.hc.acoustics.widget.CommonPopupWindow
import com.yc.yclibrary.base.YcAppCompatActivity
import com.yc.yclibx.adapter.YcAdapterHelper
import kotlinx.android.synthetic.main.formula_button.*
import kotlinx.android.synthetic.main.type2_formula1_location_activity.*
import kotlinx.android.synthetic.main.type2_formula1_location_activity.view.*
import kotlinx.android.synthetic.main.type6_formula2_location_activity.*
import kotlinx.android.synthetic.main.type6_formula3_location_activity.*
import kotlinx.android.synthetic.main.type6_formula4_location_activity.*
import kotlinx.android.synthetic.main.type6_formula6_location_activity.*
import kotlinx.android.synthetic.main.type7_formula2_location_activity.*
import kotlinx.android.synthetic.main.type7_formula5_location_activity.*
import java.lang.NumberFormatException

/**
 *  声学增益 - 最大物理距离（MPD）
 */
class Type7Formula5Activity : BaseActivity() {
    lateinit var mPopupwindow: CommonPopupWindow<String>
    var typeFormula: Int = 0

    companion object {
        private const val KEY_TITLE = "key_title"
        @JvmStatic
        fun newInstance(activity: YcAppCompatActivity, title: String) {
            val intent = Intent(activity, Type7Formula5Activity::class.java)
            intent.putExtra(KEY_TITLE, title)
            activity.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.type7_formula5_location_activity
    }

    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(intent.getStringExtra(KEY_TITLE))
        mPopupwindow = object : CommonPopupWindow<String>(this) {
            override fun onUpdate(helper: YcAdapterHelper, item: String?, position: Int) {
                helper.setText(R.id.weightPopupWindowDetailsItemTv, item)
            }
        }
        mPopupwindow.selectItem { viewHolder, view, position ->
            typeFormula = position
            formula7_5_TypeTv.setText(mPopupwindow.getItem(position))
            when (position) {
                0 -> {
                    formula7_5_a_TEV.setEdtType(true)
                    formula7_5_b_TEV.setEdtType(true)
                    formula7_5_c_TEV.setEdtType(true)
                    formula7_5_d_TEV.setEdtType(true)
                    formula7_5_e_TEV.setEdtType(false)
                }
                1 -> {
                    formula7_5_a_TEV.setEdtType(true)
                    formula7_5_b_TEV.setEdtType(true)
                    formula7_5_c_TEV.setEdtType(true)
                    formula7_5_d_TEV.setEdtType(false)
                    formula7_5_e_TEV.setEdtType(true)
                }
                2 -> {
                    formula7_5_a_TEV.setEdtType(true)
                    formula7_5_b_TEV.setEdtType(true)
                    formula7_5_c_TEV.setEdtType(false)
                    formula7_5_d_TEV.setEdtType(true)
                    formula7_5_e_TEV.setEdtType(true)
                }
                3 -> {
                    formula7_5_a_TEV.setEdtType(true)
                    formula7_5_b_TEV.setEdtType(false)
                    formula7_5_c_TEV.setEdtType(true)
                    formula7_5_d_TEV.setEdtType(true)
                    formula7_5_e_TEV.setEdtType(true)
                }
                4 -> {
                    formula7_5_a_TEV.setEdtType(false)
                    formula7_5_b_TEV.setEdtType(true)
                    formula7_5_c_TEV.setEdtType(true)
                    formula7_5_d_TEV.setEdtType(true)
                    formula7_5_e_TEV.setEdtType(true)
                }
            }
            formula7_5_a_TEV.reset()
            formula7_5_b_TEV.reset()
            formula7_5_c_TEV.reset()
            formula7_5_d_TEV.reset()
            formula7_5_e_TEV.reset()
            mPopupwindow.dismiss()
        }
        mPopupwindow.addData(
            mutableListOf(
                "最大开启话筒数量", "最大扬声器到最远听音人的距离", "最大讲话者到话筒的距离"
                , "最小话筒到扬声器的距离", "最小等效声学距离EAD"
            )
        )
        formula7_5_TypeTv.setOnClickListener {
            mPopupwindow.showAsDropDown(it)
        }
        formulaResetBtn.setOnClickListener {
            formula7_5_a_TEV.reset()
            formula7_5_b_TEV.reset()
            formula7_5_c_TEV.reset()
            formula7_5_d_TEV.reset()
            formula7_5_e_TEV.reset()
        }

        formulaOkBtn.setOnClickListener {
            when (typeFormula) {
                0 -> {
                    try {
                        val resultE = FormulaUtil.formula7_5_1(
                            formula7_5_a_TEV.getDouble(), formula7_5_b_TEV.getDouble()
                            , formula7_5_c_TEV.getDouble(), formula7_5_d_TEV.getDouble()
                        )
                        formula7_5_e_TEV.setText(resultE)
                    } catch (e: NumberFormatException) {
                        formula7_5_e_TEV.setText("Infinity")
                    }
                }
                1 -> {
                    try {
                        val resultD = FormulaUtil.formula7_5_2(
                            formula7_5_a_TEV.getDouble(), formula7_5_b_TEV.getDouble()
                            , formula7_5_c_TEV.getDouble(), formula7_5_e_TEV.getDouble()
                        )
                        formula7_5_d_TEV.setText(resultD)
                    } catch (e: NumberFormatException) {
                        formula7_5_d_TEV.setText("Infinity")
                    }
                }
                2 -> {
                    try {
                        val resultC = FormulaUtil.formula7_5_3(
                            formula7_5_a_TEV.getDouble(), formula7_5_b_TEV.getDouble()
                            , formula7_5_d_TEV.getDouble(), formula7_5_e_TEV.getDouble()
                        )
                        formula7_5_c_TEV.setText(resultC)
                    } catch (e: NumberFormatException) {
                        formula7_5_c_TEV.setText("Infinity")
                    }
                }
                3 -> {
                    try {
                        val resultB = FormulaUtil.formula7_5_4(
                            formula7_5_a_TEV.getDouble(), formula7_5_c_TEV.getDouble()
                            , formula7_5_d_TEV.getDouble(), formula7_5_e_TEV.getDouble()
                        )
                        formula7_5_b_TEV.setText(resultB)
                    } catch (e: NumberFormatException) {
                        formula7_5_b_TEV.setText("Infinity")
                    }
                }
                4 -> {
                    try {
                        val resultA = FormulaUtil.formula7_5_5(
                            formula7_5_b_TEV.getDouble(), formula7_5_c_TEV.getDouble()
                            , formula7_5_d_TEV.getDouble(), formula7_5_e_TEV.getDouble()
                        )
                        formula7_5_a_TEV.setText(resultA)
                    } catch (e: NumberFormatException) {
                        formula7_5_a_TEV.setText("Infinity")
                    }
                }
            }

        }
    }
}